﻿/**************************************************************************
创建日期:	2019/10/30 13:16:11     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
Copyright (c)  ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.DbCore.Cores;
using ZhCun.WebCommon.AL;
using ZhCun.WebCommon.ViewModels;
using MyConsume.Models;
using ZhCun.Utils.Helpers;
using System.Linq;

namespace MyConsume.AL
{
    public class ALAccount : ALCRUDBase<ArgGetList, TAccount>
    {
        protected override bool CheckSaveArg(TAccount model, bool isAdd, out string msg)
        {
            if (!model.ActName.IsLenRange(2, 10))
            {
                msg = "账户名称长度在2-10之间";
                return false;
            }
            if (!model.ActName.IsLenRange(2, 10))
            {
                msg = "助记码长度在2-10之间";
                return false;
            }
            if (DB.QueryExist<TAccount>(s => s.ActName == model.ActName && s.Id != model.Id))
            {
                msg = "账户名称已经存在";
                return false;
            }

            msg = null;
            return true;
        }

        protected override ExecResult GetDelResult(string id)
        {
            TAccount t = new TAccount();
            t.Id = id;
            t.IsDel = true;
            return DB.Update<TAccount>(t);
        }

        protected override void SetQueryOption(ArgGetList arg, QueryCondition<TAccount> query)
        {
            query.WhereAnd(s => s.IsDel == true);
            if (!arg.searchValue.IsEmpty())
            {
                string likeStr = $"%{arg.searchValue}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.ActName, likeStr) ||
                    s.WEx_Like(s.ActNamePY, likeStr));
            }
        }

        public bool ExecOtherApi(out string msg)
        {
            throw new NotImplementedException();
        }
    }
}